<?php defined ( 'SYSPATH' ) or die ( 'No direct access allowed.' );
/**
 * 
 * 废品仓库成本明细报表
 * Enter description here ...
 * @author locove
 *
 */
class Controller_Report_Waster extends Controller_Admin
{
    public function before()
    {
    	try {
    		  ACL::Required('waster report');
    	} catch ( HTTP_Exception_403 $e ) {
				Message::error('sorry，你没有权限进这个目录');
    		$this->request->redirect ( URL::base (true) );
    	}
        parent::before();
    }
    
    /**
     * 首页搜索界面
     * Enter description here ...
     */
	public function action_index()
	{
		$this->title=__("废品仓库成本明细");
		$view = View::factory('report/waster');
		$this->response->body($view);
	}

	/**
	 * 获取分页报废产品信息
	 */
	public function action_waster_load()
	{
		$offset=0;
		$items_per_page=100;
	 	$result=ORM::factory('waster_receipt');
		if(isset($_GET['page'])&&$_GET['page']){
	 		$offset=($_GET['page']-1)*$items_per_page;
		}
	 	if(isset($_GET['sku'])&&$_GET['sku']){
	 		$result	->where('sku', '=', $_GET['sku']);
	 	}
		if(isset($_GET['start_time'])&&$_GET['start_time']){
	 		$result->where('waster_receipts_time','>=',$_GET['start_time']);
	 	}
	 	if(isset($_GET['end_time'])&&$_GET['end_time']){
	 		$result->where('waster_receipts_time','<=',$_GET['end_time']);
	 	}
	 	if(isset($_GET['sortcondition'])&&$_GET['sortcondition']){
	 		if(isset($_GET['order'])&&$_GET['order']){
		 		$result->order_by($_GET['sortcondition'],$_GET['order']==1?'asc':'desc');
	 		}else{
	 			$result->order_by($_GET['sortcondition'],'DESC');
	 		}
	 	}
	 	$result_count = $result;
	 	$result_all=$result->find_all()->as_array();
	 	$count = count($result_all);
 		$pagination = Pagination::factory(array(
            'items_per_page' => $items_per_page, 
            'total_items' => $count,
	        'uri'=> $this->request->uri(),
        ));
        //重复的地方(这里如果不重复就是出现$result为无条件时候的对象,展示所有)
	 	if(isset($_GET['sku'])&&$_GET['sku']){
	 		$result	->where('sku', '=', $_GET['sku']);
	 	}
		if(isset($_GET['start_time'])&&$_GET['start_time']){
	 		$result->where('waster_receipts_time','>=',$_GET['start_time']);
	 	}
	 	if(isset($_GET['end_time'])&&$_GET['end_time']){
	 		$result->where('waster_receipts_time','<=',$_GET['end_time']);
	 	}
	 	if(isset($_GET['sortcondition'])&&$_GET['sortcondition']){
	 		if(isset($_GET['order'])&&$_GET['order']){
		 		$result->order_by($_GET['sortcondition'],$_GET['order']==1?'asc':'desc');
	 		}else{
	 			$result->order_by($_GET['sortcondition'],'DESC');
	 		}
	 	}
 		$wasters=$result->limit($pagination->items_per_page)
        	->offset($offset)->find_all()->as_array();
 		$view=View::factory('report/waster_table')
 			->set('wasters',$wasters)
 			->set('totles',$count)
 			->render();
 		echo $view;
	}
	
	/**
	 * 下载报表
	 */
	public function action_down()
	{
 		set_time_limit(0);
 		ini_set('memory_limit','512M');
 		require_once(Kohana::find_file('vendor', 'PHPExcel/PHPExcel/IOFactory'));
 		$this->objReader  = PHPExcel_IOFactory::createReader('Excel5');
 		$objPHPExcel      = new PHPExcel();
 		$objPHPExcel->getProperties()->setCreator("YKS")
 		->setLastModifiedBy("YKS")
 		->setTitle("Office 2003 XLSX Test Document")
 		->setSubject("Office 2003 XLSX Test Document")
 		->setDescription("Test document for Office 2003 XLSX, generated using PHP classes.")
 		->setKeywords("office 2003 openxml php")
 		->setCategory("Test result file");
 		$objActSheet =$objPHPExcel->setActiveSheetIndex(0);
 		$objActSheet->setCellValue('A1', 'SKU');
 		$objActSheet->setCellValue('B1', '中文名称');
 		$objActSheet->setCellValue('C1', '数量');
 		$objActSheet->setCellValue('D1', '单价');
 		$objActSheet->setCellValue('E1', '金额');
 		$objActSheet->setCellValue('F1', '成本');
 		$objActSheet->setCellValue('G1', '备注');
 		$result=ORM::factory('waster_receipt');
 		if(isset($_GET['sku'])&&$_GET['sku']){
	 		$result	->where('sku', '=', $_GET['sku']);
	 	}
		if(isset($_GET['start_time'])&&$_GET['start_time']){
	 		$result->where('waster_receipts_time','>=',$_GET['start_time']);
	 	}
	 	if(isset($_GET['end_time'])&&$_GET['end_time']){
	 		$result->where('waster_receipts_time','<=',$_GET['end_time']);
	 	}
	 	if(isset($_GET['sortcondition'])&&$_GET['sortcondition']){
	 		if(isset($_GET['order'])&&$_GET['order']){
		 		$result->order_by($_GET['sortcondition'],$_GET['order']==1?'asc':'desc');
	 		}else{
	 			$result->order_by($_GET['sortcondition'],'DESC');
	 		}
	 	}
 		$wasters=$result->find_all()->as_array();
 		$i=0;
        foreach($wasters as $waster){
 			$key=$i+2;
 			$i++;
 			$objActSheet->setCellValue('A'.$key, $waster->sku);
	 		$objActSheet->setCellValue('B'.$key, $waster->sku_name);
	 		$objActSheet->setCellValue('C'.$key, $waster->quantity);
			$objActSheet->setCellValue('D'.$key, $waster->single_price);
	 		$objActSheet->setCellValue('E'.$key, round($waster->cost,2));
	 		$objActSheet->setCellValue('F'.$key, round($waster->money,2));
	 		$objActSheet->setCellValue('G'.$key, $waster->remark);
 		}
 		$objPHPExcel->getActiveSheet()->setTitle('废品仓库成本明细报表');
		$objPHPExcel->setActiveSheetIndex(0);
 		$day=date("m-d");
 		$filename=$day.'废品仓库成本明细报表'.$page.'.xls';
        ob_end_clean();
		header("Content-Type: application/vnd.ms-excel; charset=utf-8");
		header('Content-Disposition: attachment;filename='.$filename);
		header('Cache-Control: max-age=0');
		$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
		$objWriter->save('php://output');
		exit;		
	}
}
